package com.dwj.auto.comment.common.security;

import com.dwj.auto.comment.common.response.Response;
import com.dwj.auto.comment.common.response.ResultEnum;
import com.dwj.auto.comment.common.utils.JSONResponseUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName UserAuthenticationEntryPointHandler
 * @Description 未登录处理类
 * @Author dwjian
 * @Date 2020/7/9 20:13
 */
@Component
public class UserAuthenticationEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Response<String> response = Response.error(ResultEnum.NO_LOGIN.getCode(), ResultEnum.NO_LOGIN.getMsg());
        httpServletResponse.setStatus(401);
        JSONResponseUtils.build(httpServletResponse, response);
    }
}
